<?php

namespace app\sales\controller;

use app\admin\common\Base;
use think\Db;
//销售订单跟踪表
class Salesordertracking extends Base
{
   public function index()
   {
      $data = $this->request->param();
      extract($data);
      $month = $this->get_month_start_to_end();
      $this->assign('month', $month);

      if (isset($data['ajax']) && $data['ajax'] == 100) {
         $page = isset($data['page']) ? $data['page'] : 1;
         $page = intval($page);
         $limit = isset($data['rows']) ? $data['rows'] : 10;
         $limit = intval($limit);
         $start = $limit * ($page - 1);

         if (isset($data['startdate']) && isset($data['enddate'])) {
            $month = $this->get_month_start_to_end($data['startdate'], $data['enddate']);
         } else {
            $month = $this->get_month_start_to_end();
         }
         $sql = Db::name('order_good')->alias('a')->join('order b', 'a.order_id = b.id')->where('o_type', 0)->where('b.addtime', '>=', $month['month_start'])->where('b.addtime', '<=', $month['month_end'])->where(['b.is_del' => 0, 'b.enterprise_id' => $this->EnterpriseId]);
         $sql = !isset($data['supplierid']) || $data['supplierid'] == '' ? $sql : $sql->where('b.user_id', $data['supplierid']);
         $sql = !isset($data['goodsid']) || $data['goodsid'] == '' ? $sql : $sql->where('a.good_id', $data['goodsid']);

         // 高级搜索
         if (!isset($staff_id) || $staff_id == 0 || $staff_id == '') {
         } else {
            $c = Db::name('auth_user')->where('id', 'in', $staff_id)->select();
            $c['ids'] = implode(',', array_column($c, 'id'));
            $sql->where('b.by_admin', 'in', $c['ids']); //商品类别
         }

         $sql = !isset($sn) || $sn == '' ? $sql : $sql->where('b.sn', $sn);

         if (!isset($data['customer_category_id']) || $data['customer_category_id'] == 0 || $data['customer_category_id'] == '') {
         } else {
            $c = Db::name('customer_category')->where('id', $data['customer_category_id'])->select();
            $c['ids'] = implode(',', array_column($c, 'id'));
            $sql->where('b.user_id', 'in', $c['ids']); //商品类别
         }

         $sql = (isset($jh_time_start) && $jh_time_start) == '' ? $sql : $sql->where('jh_time', '>=', strtotime($jh_time_start));
         $sql = (isset($jh_time_end) && $jh_time_end) == '' ? $sql : $sql->where('jh_time', '<=', strtotime($jh_time_end));

         $arrStatus = $status ?? false ? explode(',', $status) : false;
         $order = '';
         if ($arrStatus) {
            $a_status = [];
            array_walk($arrStatus, function ($v) use ($sql, &$a_status, &$order) {
               switch ($v) {
                  case 1:
                     $a_status[] = 0;
                     break;
                  case 3:
                     $a_status[] = 1;
                     break;
                  case 4:
                     $sql = $sql->where('b.is_close', 1);
                     break;
                  case 5:
                     $order = 'a.good_id';
                     break;
                  case 6:
                     $order = 'b.sn';
                     break;
                  default:
                     # code...
                     break;
               }
            });
            $sql = empty($a_status) ? $sql : $sql->where('b.status', 'in', $a_status);
         }
         //分组
         $sql = $order == '' ? $sql : $sql->order($order);
         $list0 = $sql->select();
         $count = count($list0);
         $list = array_slice($list0, $start, $limit);
         //halt($list);
         foreach ($list as $key => $value) {
            $goods = Db::name('goods')->where('id', $value['good_id'])->find();
            $list[$key]['goods_sn'] = $goods['sn'];
            $list[$key]['goods_name'] = $goods['name'];
            $list[$key]['goods_model'] = $goods['model'];
            $list[$key]['unit_name'] = Db::name('unit')->where('id', $value['unit_id'])->value('name');
            $list[$key]['addtime'] = date('Y-m-d', $value['addtime']);
            $user = Db::name('customer')->where('id', $value['user_id'])->find();
            $list[$key]['user_name'] = $user['sn'] . ' ' . $user['name'];
            $list[$key]['store_name'] = Db::name('store')->where('id', $value['store_id'])->value('name');
            $list[$key]['jh_time'] = date('Y-m-d', $value['jh_time']);
            $list[$key]['saler_name'] = Db::name('auth_user')->where('id', $value['by_admin'])->value('name');
            $list[$key]['status'] = $value['status'] == 0 ? '未审核' : '已审核';
            $list[$key]['shenhe_time'] = date('Y-m-d', $value['shenhe_time']);
         }
         //halt($list);
         return $this->grid_data($list, $count, $page, $limit);
      }

      $customer = Db::name('customer')->where('is_customer', 'in', '1,3')->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
      $goods = Db::name('goods')->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();

      $this->assign('customer', $customer);
      $this->assign('goods', $goods);
      return $this->fetch();
   }
}
